#define  _CRT_SECURE_NO_WARNINGS 1

#include "List.h"

ListNode* BuyListNode()
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc newnode");
		exit(-1);
	}

	newnode->next = NULL;
	newnode->prev = NULL;

	return newnode;
}

ListNode* ListInit()
{
	ListNode* head = BuyListNode();

	head->next = head;
	head->prev = head;
	head->val = -1;
}
void ListPrint(ListNode* head)
{
	ListNode* cur = head->next;
	while (cur != head)
	{
		printf("%d ", cur->val);

		cur = cur->next;
	}

	printf("\n");
}
void ListPushBack(ListNode* head, LTDateType x)
{
	ListInsert(head, x);
}
void ListPushFront(ListNode* head, LTDateType x)
{
	ListInsert(head->next, x);
}
void ListPopBack(ListNode* head)
{
	ListErase(head->prev);
}
void ListPopFront(ListNode* head)
{
	ListErase(head->next);
}

void ListInsert(ListNode* pos, LTDateType x)
{
	assert(pos);

	ListNode* node = BuyListNode();

	node->val = x;

	pos->prev->next = node;
	node->prev = pos->prev;
	
	node->next = pos;
	pos->prev = node;
}
void ListErase(ListNode* pos)
{
	assert(!ListEmpety(pos));
	assert(pos);

	ListNode* prev = pos->prev;
	ListNode* next = pos->next;

	free(pos);

	prev->next = next;
	next->prev = prev;
}

bool ListEmpety(ListNode* head)
{
	return (head->next == head);
}